package top.lywivan.service.impl;

import top.lywivan.dao.BrandDao;
import top.lywivan.pojo.Brand;
import top.lywivan.pojo.PageBean;
import top.lywivan.service.BrandService;
import top.lywivan.utils.MapperUtil;

import java.util.List;

@SuppressWarnings("all")
public class BrandServiceImpl implements BrandService {
    @Override
    public List<Brand> queryAllBrand() {
        BrandDao brandDao = MapperUtil.getMapper(BrandDao.class);
        List<Brand> brandList = null;
        try {
            brandList = brandDao.queryAllBrand();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            MapperUtil.close();
        }
        return brandList;
    }

    @Override
    public void addBrand(Brand brand) {
        BrandDao brandDao = MapperUtil.getMapper(BrandDao.class);
        try {
            brandDao.addBrand(brand);
            MapperUtil.commitAndClose();
        } catch (Exception e) {
            e.printStackTrace();
            MapperUtil.rollbackAndClose();
            throw new RuntimeException(e);
        }
    }

    @Override
    public void deleteBrandByIds(int[] ids) {
        BrandDao brandDao = MapperUtil.getMapper(BrandDao.class);
        try {
            brandDao.deleteBrandByIds(ids);
            MapperUtil.commitAndClose();
        } catch (Exception e) {
            e.printStackTrace();
            MapperUtil.rollbackAndClose();
            throw new RuntimeException(e);
        }
    }

    @Override
    public PageBean queryPageBean(Integer currentPage, Integer pageSize) {
        BrandDao brandDao = MapperUtil.getMapper(BrandDao.class);
        PageBean<Brand> pageBean = null;
        try {
            pageBean = new PageBean<>();
            Long totalCount = brandDao.queryTotalCount();
            pageBean.setTotalCount(totalCount);
            List<Brand> pageList = brandDao.queryPageList((currentPage - 1) * pageSize, pageSize);
            pageBean.setPageList(pageList);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            MapperUtil.close();
        }
        return pageBean;
    }

    @Override
    public PageBean<Brand> queryPageBeanWhere(Brand brand, Integer currentPage, Integer pageSize) {
        BrandDao brandDao = MapperUtil.getMapper(BrandDao.class);
        PageBean<Brand> pageBean = null;
        try {
            pageBean = new PageBean<>();
            String brandName = brand.getBrandName();
            String companyName = brand.getCompanyName();
            if(brandName!=null&&!brandName.trim().equals("")) {
                brand.setBrandName("%" + brandName + "%");
            }
            if(companyName!=null&&!companyName.trim().equals("")) {
                brand.setCompanyName("%" + companyName + "%");
            }
            Long totalCount = brandDao.queryTotalCountWhere(brand);
            List<Brand> brandList = brandDao.queryPageListWhere(brand, (currentPage - 1) * pageSize, pageSize);
            pageBean.setTotalCount(totalCount);
            pageBean.setPageList(brandList);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            MapperUtil.close();
        }
        return pageBean;
    }

    @Override
    public void deleteBrandById(Integer id) {
        BrandDao brandDao = MapperUtil.getMapper(BrandDao.class);
        try {
            brandDao.deleteBrandById(id);
            MapperUtil.commitAndClose();
        } catch (Exception e) {
            e.printStackTrace();
            MapperUtil.rollbackAndClose();
            throw new RuntimeException(e);
        }
    }

    @Override
    public Brand queryBrandById(Integer id) {
        BrandDao brandDao = MapperUtil.getMapper(BrandDao.class);
        Brand brand = null;
        try {
            brand = brandDao.queryBrandById(id);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            MapperUtil.close();
        }
        return brand;
    }

    @Override
    public void updateBrandById(Brand brand) {
        BrandDao brandDao = MapperUtil.getMapper(BrandDao.class);
        try {
            brandDao.updateBrandById(brand);
            MapperUtil.commitAndClose();
        } catch (Exception e) {
            e.printStackTrace();
            MapperUtil.rollbackAndClose();
            throw new RuntimeException(e);
        }
    }
}
